package com.customization.unionpay.market.workflowaction;

import com.alibaba.fastjson.JSONObject;
import com.customization.unionpay.market.util.FormDataUtils;
import com.customization.unionpay.market.util.InnovativeSettlementUtils;
import weaver.integration.logging.Logger;
import weaver.integration.logging.LoggerFactory;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

import java.util.Map;

/**
 * @author sjm 营销退款相关节点action
 * @description
 */
public class MarketingRefundAction implements Action {

    private static final Logger log = LoggerFactory.getLogger(MarketingRefundAction.class);

    @Override
    public String execute(RequestInfo requestInfo) {
        try {

            Map<String, Object> allFormData = FormDataUtils.getAllFormData(requestInfo);

            Map<String, String> mainMap = (Map<String, String>) allFormData.get("mainMap");

            // 退款单号            tkdh
            String bdh = mainMap.get("bdh");
            log.info("bdh："+bdh);
            // 账套号              zth
            String zth = mainMap.get("zth");
            log.info("zth："+zth);
            // 付款业务账号         fkywzh
            String fkywzh = mainMap.get("fkywzh");
            log.info("fkywzh："+fkywzh);
            // 付款业务账号         yxpthdsqm
            String yxpthdsqm = mainMap.get("yxpthdsqm");
            log.info("yxpthdsqm："+yxpthdsqm);
            // 所有明细表的值
            Map<String,Map <String, Map<String, String>>>  detailMap = (Map<String, Map<String, Map<String, String>>>) allFormData.get("detailMap");

            Map<String, Map<String, String>> dt1 = detailMap.get("dt1");

            String msgStr = null;
            for (String key : dt1.keySet()) {
                Map<String, String> value = dt1.get(key);
                // 退款账户开户行行号
                String tkzhkhhhh = value.get("tkzhkhhhh");
                // 退款账户开户行
                String tkzhkhh = value.get("tkzhkhh");
                // 退款账户名称
                String tkzhmc = value.get("tkzhmc");
                // 退款账号
                String tkzh = value.get("tkzh");
                // 退款原因
                String tkyy = value.get("tkyy");
                // 本次申请退款金额
                String bcsqtkje = value.get("bcsqtkje");

                JSONObject mpmKeyJson = new JSONObject();
                // 是 业务流水号	字母数字组合
                mpmKeyJson.put("mpmKey",bdh);
                // 	是	账套号	字母数字组合
                mpmKeyJson.put("sfbCd",zth);
                // 	是	付款业务账号	数字
                mpmKeyJson.put("bussAcctNo",fkywzh);
                //  是	币种 (人民币：CNY)	字母数字组合
                mpmKeyJson.put("currCd","CNY");
                //	是	金额（单位为分）	数字
                mpmKeyJson.put("bussAt",!"".equals(bcsqtkje) ? String.valueOf(Double.valueOf(bcsqtkje) * 100).split("\\.")[0] : "0");
                //	是	收款账户账号	数字
                mpmKeyJson.put("rcvBankAcctNo",tkzh);
                //	是	收款账户账号名称
                mpmKeyJson.put("rcvBankAcctNm",tkzhmc);
                //	是	收款账户账号行号	数字
                mpmKeyJson.put("rcvBankAcctBankNo",tkzhkhhhh);
                //	是	收款账户账号行名
                mpmKeyJson.put("rcvBankAcctBankNm",tkzhkhh);
                //	否	附言
                mpmKeyJson.put("transPtscpt",tkyy);
                //	是	营销活动授权码	字母数字组合
                mpmKeyJson.put("marketAuthorCode",yxpthdsqm);

                String resultStr = InnovativeSettlementUtils.createWorkFlow(mpmKeyJson);
                log.info("resultStr："+resultStr);
                JSONObject resultJson = JSONObject.parseObject(resultStr);

                String code = resultJson.getString("code");
                if ("0000".equals(code)) {

                } else {
                    String msg = resultJson.getString("msg");
                    log.error("请求失败：" + msg);
                    if(msgStr == null){
                        msgStr = "";
                        msgStr += "退款账号："+tkzh+"相关信息，推送创新结算平台失败："+msg;
                    }else {
                        msgStr += ",退款账号："+tkzh+"相关信息，推送创新结算平台失败："+msg;
                    }
                }
            }
            if(msgStr != null){
                requestInfo.getRequestManager().setMessageid("10001");
                requestInfo.getRequestManager().setMessagecontent(msgStr);
                return Action.FAILURE_AND_CONTINUE;
            }
        } catch (Exception e) {
            // 异常报错是填写异常信息，阻止流程继续流转
            requestInfo.getRequestManager().setMessageid("10001");
            requestInfo.getRequestManager().setMessagecontent(e.getMessage());
            return Action.FAILURE_AND_CONTINUE;
        }
        return Action.SUCCESS;
    }

}
